from pages.SystemManage.SystemManage import SystemManage
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException
from common.GetLoc import CityPageLoc


class CityPage(SystemManage):
    loc_cityName = CityPageLoc().get_loc_cityName()

    def __init__(self, browser):
        super().__init__(browser)
        self.login()
        self.find_ele(self.loc_systemManage).click()
        self.find_ele(self.loc_cityList).click()

    def add_city(self, city_name):
        self.find_ele(self.loc_addButton).click()
        self.find_ele(self.loc_cityName).send_keys(city_name)
        self.find_ele(self.loc_confirmButton).click()
        try:
            assert city_name == self.find_ele(self.loc_lastContent).text, '添加失败'
        except AssertionError:
            return False
        else:
            return True

    def edit_city(self, old_name, new_name):
        self.chose_checkBox(old_name)
        self.find_ele(self.loc_editButton).click()
        self.find_ele(self.loc_cityName).clear()
        self.find_ele(self.loc_cityName).send_keys(new_name)
        self.find_ele(self.loc_confirmButton).click()
        try:
            assert new_name == self.find_info(new_name), '修改失败'
        except NoSuchElementException and AssertionError:
            return False
        else:
            return True

    def delete_city(self, city_name):
        self.chose_checkBox(city_name)
        self.find_ele(self.loc_delButton).click()
        self.find_ele(self.loc_alertButton).click()
        try:
            self.find_ele(self.loc_checkBox % city_name).click()
        except StaleElementReferenceException:
            return True
        else:
            return False


if __name__ == '__main__':
    a = CityPage(webdriver.Chrome())
    a.add_city('贵州省')
    # a.edit_city('11', '13')
    # if a.delete_city('贵阳'):
    #     print('断言对了')
    b = input()
